// pages/userCenter.js
const app = getApp();
Page({
  data: {
    userImg:'',
    isLogin:false,
    userInfo:null
  },
  onLoad() { 
    this.setData(
      {
        isLogin: !!app.globalData.token,
        userInfo:app.globalData.userInfo,
        userImg:app.globalData.userInfo ? app.globalData.userInfo.avatar : ''
      }
    );
  },
  onShow(){
    this.setData(
      {
        isLogin: !!app.globalData.token,
        userInfo:app.globalData.userInfo,
      }
    );
  },
  goLogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  //退出登录
  loginOut(){
    let _this = this;
    wx.request({
      url: app.globalData.baseServePath + '/wxapi/logout',
      data:{
        token:app.globalData.token
      },
      success(res){
        app.globalData.userInfo = '';
        app.globalData.token = '';
        wx.setStorage({
          key:'token',
          data:'',
          success(){  
            wx.setStorage({
              key:'userInfo',
              data:'',
              success(){  
                wx.showToast({
                  title: '退出登录成功',
                  icon: 'success',
                  duration: 2000,
                  success(res){
                    setTimeout(()=>{
                      _this.setData({isLogin:false});
                    },1000)
                  }
                })
              }
            })
          }
        })       
      }
    })
  },
  // 获取个人信息
  getUserInfo(){
    //需要先登录
    if(!app.globalData.token){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/userInfo/userInfo?phonenumber='+ this.data.userInfo.phonenumber
      })
    }   
  },
  addOpinons(){
    //需要先登录
    if(!app.globalData.token){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/opinions/opinions',
      })
    }   
  },

  changePassword(){
    //需要先登录
    if(!app.globalData.token){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/changePassword/changePassword',
      })
    }   
  },
  aboutOus(){
    wx.navigateTo({
      url: '/pages/aboutOus/aboutOus',
    })
  }
})
